<?php
declare (strict_types = 1);

namespace app\command;

use app\api\model\User;


use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
use think\facade\Log;

class FixErrorCommand extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('fix_error')
            ->setDescription('the fix_error command');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $output->writeln('fix_error');

        Db::startTrans();
        try {
            $this->executeUserLevel();
            $this->executeDirectorLevel();
            $this->executeHonorDirectorLevel();

            Db::commit();
        } catch (\Exception $exception) {

            Db::rollback();

            Log::error('afterShopping:'.json_encode(['message' => $exception->getMessage(), 'file' => $exception->getFile(), 'line' => $exception->getLine()], JSON_UNESCAPED_UNICODE));
            return false;
        }

        return true;
    }

    public function executeUserLevel(){
        //自动处理等级
        $userList = User::field('id, spending, performance, status, level_id, virtual_level_id')
            ->where('status', 'on')
            ->where('level_id', '>', 0)
//            ->order('total_num DESC')
            ->select();
        foreach ($userList as $item){
//            echo $item['id'] . ':' . $item['total_amount'] . ',';
            $currentUserChildren = User::field('id, pid, status, spending+performance as total_amount')
                ->where('status', 'on')
                ->where('pid', $item['id'])
                ->order('total_amount DESC')
                ->find();
            if(!empty($currentUserChildren)){
                //如果有分支
                //剩余团队业绩
                $remain_amount = $item['performance'] - $currentUserChildren['total_amount'];
                if($item['performance'] >= 127680000)
                {
                    if($remain_amount >= 39900000){
                        User::where('id', $item['id'])->update([
                            'level_id'=>5,
                            'virtual_level_id'=>max($item['virtual_level_id'], 5)
                        ]);
                        continue;
                    }
                }
                if($item['performance'] >= 47880000)
                {
                    if($remain_amount >= 7980000){
                        User::where('id', $item['id'])->update([
                            'level_id'=>4,
                            'virtual_level_id'=>max($item['virtual_level_id'], 4)
                        ]);
                        continue;
                    }
                }
                if($item['performance'] >= 3990000)
                {
                    if($remain_amount >= 798000){
                        User::where('id', $item['id'])->update([
                            'level_id'=>3,
                            'virtual_level_id'=>max($item['virtual_level_id'], 3)
                        ]);
                        continue;
                    }
                }
                if($item['performance'] >= 399000)
                {
                    User::where('id', $item['id'])->update([
                        'level_id'=>2,
                        'virtual_level_id'=>max($item['virtual_level_id'], 2)
                    ]);
                    continue;
                }
            }
        }
        return true;
    }

    public function executeDirectorLevel(){
        //自动处理等级
        $userList = User::field('id, status, level_id, virtual_level_id')
            ->where('status', 'on')
            ->where('level_id', '=', 5)
            ->select();
        foreach ($userList as $item) {
            $res = self::calcDirector($item['id'], 5);
            if ($res === true) {
                User::where('id', $item['id'])->update([
                    'level_id' => 6,
                    'virtual_level_id'=>max($item['virtual_level_id'], 6)
                ]);
            }
        }
        return true;
    }

    public function calcDirector($p_user_id, $level_id)
    {
        //计算是否符合条件的用户
        //找该账号下所有直推 且是合伙人的
        $where = [];
        //$where[] = ['pid', '=', $p_user_id];
        //$where[] = ['level_id', '=', 5];
        $where[] = ['status', '=', 'on'];

        $count = User::where($where)
            ->where('pid', '=', $p_user_id)
            ->where('level_id', '>=', $level_id)
            ->count();
        if($count >= 2){
            return true;
        }else{
            $user_lists = User::field('id, pid, parent_ids, level_id, status')
                ->where($where)
                ->where('pid', '=', $p_user_id)
                ->select();
            $total_current_children_count = 0;
            foreach ($user_lists as $user)
            {
                if($user['level_id'] >= $level_id){
                    $total_current_children_count += 1;
                    break;
                }
                if ($total_current_children_count >= 2)
                {
                    return true;
                }
//                $str = '%|' . $user['id'] . '|' . $p_user_id . '|%';
                $str = '%|' . $user['id'] . '|%';
                $current_children_count = User::where($where)
                    ->whereLike('parent_ids', $str)
                    ->where('level_id', '>=', $level_id)
                    ->count();
                if ($current_children_count > 0)
                {
                    $total_current_children_count += 1;
                    break;
                }
            }
        }
        return false;
    }

    public function executeHonorDirectorLevel(){
        //自动处理等级
        //自动处理等级
        $userList = User::field('id, status, level_id, virtual_level_id')
            ->where('status', 'on')
            ->where('level_id', '=', 6)
            ->select();
        foreach ($userList as $item) {
            $res = self::calcDirector($item['id'], 6);
            if ($res === true) {
                User::where('id', $item['id'])->update([
                    'level_id' => 7,
                    'virtual_level_id'=>max($item['virtual_level_id'], 7)
                ]);
            }
        }
        return true;
    }

    //    public function executeUserLevelOld(){
//        //自动处理等级
//        $userList = User::field('id, spending_num, performance_num, spending_num+performance_num as total_num, level_id')
//            ->where('status', 'on')
//            ->where('level_id', '>', 0)
//            ->order('total_num DESC')
//            ->select();
//        foreach ($userList as $item){
////            echo $item['id'] . ':' . $item['total_amount'] . ',';
//            $currentUserChildren = User::field('id, spending_num+performance_num as total_num')
//                ->where('status', 'on')
//                ->where('pid', $item['id'])
//                ->order('total_num DESC')
//                ->find();
//            if(!empty($currentUserChildren)){
//                //如果有分支
//                $remain_num = $item['total_num'] - $currentUserChildren['total_num'];
//                if($item['spending_num'] >= 3200)
//                {
//                    if($remain_num >= 1000){
//                        User::where('id', $item['id'])->update([
//                            'level_id'=>5
//                        ]);
//                        continue;
//                    }
//                }
//                if($item['spending_num'] >= 1200)
//                {
//                    if($remain_num >= 200){
//                        User::where('id', $item['id'])->update([
//                            'level_id'=>4
//                        ]);
//                        continue;
//                    }
//                }
//                if($item['spending_num'] >= 100)
//                {
//                    if($remain_num >= 20){
//                        User::where('id', $item['id'])->update([
//                            'level_id'=>3
//                        ]);
//                        continue;
//                    }
//                }
//            }
//        }
//        return true;
//    }

//    public function executeUserLevelOld2(){
//        //自动处理等级
//        $userList = User::field('id, spending, performance, status, level_id')
//            ->where('status', 'on')
//            ->where('level_id', '>', 0)
////            ->order('total_num DESC')
//            ->select();
//        foreach ($userList as $item){
////            echo $item['id'] . ':' . $item['total_amount'] . ',';
//            $currentUserChildren = User::field('id, pid, status, spending+performance as total_amount')
//                ->where('status', 'on')
//                ->where('pid', $item['id'])
//                ->order('total_amount DESC')
//                ->find();
//            if(!empty($currentUserChildren)){
//                //如果有分支
//                //剩余团队业绩
//                $remain_amount = $item['performance'] - $currentUserChildren['total_amount'];
//                if($item['performance'] >= 127680000)
//                {
//                    if($remain_amount >= 39900000){
//                        User::where('id', $item['id'])->update([
//                            'level_id'=>5
//                        ]);
//                        continue;
//                    }
//                }
//                if($item['performance'] >= 47880000)
//                {
//                    if($remain_amount >= 7980000){
//                        User::where('id', $item['id'])->update([
//                            'level_id'=>4
//                        ]);
//                        continue;
//                    }
//                }
//                if($item['performance'] >= 3990000)
//                {
//                    if($remain_amount >= 798000){
//                        User::where('id', $item['id'])->update([
//                            'level_id'=>3
//                        ]);
//                        continue;
//                    }
//                }
//                if($item['performance'] >= 399000)
//                {
//                    User::where('id', $item['id'])->update([
//                        'level_id'=>2
//                    ]);
//                    continue;
//                }
//            }
//        }
//        return true;
//    }
}
